package com.twu.biblioteca.model;

import com.twu.biblioteca.dao.memory.MemoryMovieDao;
import com.twu.biblioteca.entity.Movie;
import com.twu.biblioteca.exception.CheckoutFailException;
import com.twu.biblioteca.exception.ReturnFailException;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.List;

import static org.fest.assertions.api.Assertions.assertThat;

public class MovieModelIT {
  private MovieModel sut;
  private Movie movieToCheckout;

  @BeforeMethod
  public void setUp() throws Exception {
    sut = new MovieModel(new MemoryMovieDao());
    movieToCheckout = new Movie("10.123/1234-5678-9ABC-DEF0-K", "Avengers", "Joss Whedon");
    movieToCheckout.setYear(2012);
    movieToCheckout.setRate(10);
  }

  @Test
  public void could_checkout_and_return_available_books() throws Exception {
    // Given

    // When
    List<Movie> availableMovies = sut.checkout(movieToCheckout);

    // Then
    assertThat(availableMovies).hasSize(4);
  }

  @Test(expectedExceptions = CheckoutFailException.class)
  public void should_throw_exception_when_checkout_request_is_invalid() throws Exception {
    // Given
    Movie wrongMovieToCheckout = new Movie("10.123/5555-5678-9ABC-DEF0-K", "Avengers2",
        "Joss Whedon");
    wrongMovieToCheckout.setYear(2012);
    wrongMovieToCheckout.setRate(10);
    // When
    sut.checkout(wrongMovieToCheckout);

    // Then
  }

  @Test(expectedExceptions = CheckoutFailException.class)
  public void could_not_checkout_not_available() throws Exception {
    // Given

    // When
    sut.checkout(movieToCheckout);
    sut.checkout(movieToCheckout);

    // Then
  }

  @Test
  public void could_return_borrowed_movie() throws Exception {
    // Given

    // When
    sut.checkout(movieToCheckout);
    List<Movie> availableMovies = sut.returnMovie(movieToCheckout);

    // Then
    assertThat(availableMovies).hasSize(5);
  }

  @Test(expectedExceptions = ReturnFailException.class)
  public void should_throw_exception_when_return_request_is_invalid() throws Exception {
    // Given

    // When
    sut.returnMovie(movieToCheckout);

    // Then
  }
}
